package com.ikas.ai.server.module.assessment.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.module.assessment.dao.AssessWfResultDetailMapper;
import com.ikas.ai.server.module.assessment.model.po.AssessWfResultDetail;
import com.ikas.ai.server.module.assessment.model.vo.AwfResultDetailCalVo;
import com.ikas.ai.server.module.assessment.model.vo.AwfResultDetailVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 工况评价结果明细表 服务实现类
 * </p>
 *
 * @author wuyf
 * @since 2023-10-31
 */
@Service
public class AssessWfResultDetailService extends ServiceImpl<AssessWfResultDetailMapper, AssessWfResultDetail> {

    public List<AwfResultDetailVo> listByRid(Long rid) {
        List<AssessWfResultDetail> list = getListByRid(rid);

        return list.stream().map(this::cover2Vo).collect(Collectors.toList());
    }

    private List<AssessWfResultDetail> getListByRid(Long rid) {
        LambdaQueryWrapper<AssessWfResultDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssessWfResultDetail::getRid, rid);
        return this.list(queryWrapper);
    }

    private AwfResultDetailVo cover2Vo(AssessWfResultDetail awfRd) {
        return BeanUtil.copyProperties(awfRd, AwfResultDetailVo.class);
    }

    private AwfResultDetailCalVo cover2CalVo(AssessWfResultDetail awfRd) {
        AwfResultDetailCalVo calVo = BeanUtil.copyProperties(awfRd, AwfResultDetailCalVo.class);
        if (Objects.nonNull(awfRd.getFailRate())) {
            calVo.setFailRate(awfRd.getFailRate()/100);
        }
        if (Objects.nonNull(awfRd.getOccurProb())) {
            calVo.setOccurProb(awfRd.getOccurProb()/100);
        }
        return calVo;
    }

    public Integer countOfResultDetailByRid(Long rid) {
        List<AssessWfResultDetail> listByRid = getListByRid(rid);
        return CollectionUtils.isEmpty(listByRid) ? 0 : listByRid.size();
    }

    /**
     * 通过评价结果id获取评价结论明细信息-
     * 返回的VO对象的概率值转成了double用于计算
     * @param rid 模型评价结果id
     * @return list of AwfResultDetailCalVo
     */
    public List<AwfResultDetailCalVo> getResultDetailCalByRid(Long rid) {
        List<AssessWfResultDetail> list = getListByRid(rid);
        //风险名称相同时保留发生概率最大的一条
        list = new ArrayList<>(list.stream().collect(
                Collectors.toMap(AssessWfResultDetail::getRiskName,
                        Function.identity(), (obj1, obj2) ->
                                ObjectUtils.defaultIfNull(obj1.getOccurProb(), 0.0) >
                                        ObjectUtils.defaultIfNull(obj2.getOccurProb(), 0.0)
                                ? obj1 : obj2
                )).values());
        return list.stream().map(this::cover2CalVo).collect(Collectors.toList());
    }
}
